#include <windows.h>
#include <shellapi.h>
#include <string>

#define ID_TASKMGR  1001
#define ID_EXPLORER 1002
#define ID_SNIPPING 1003
#define ID_PAINT    1004
#define ID_CALC     1005

const wchar_t CLASS_NAME[] = L"Win32WindowToolClass";

// 全局变量
HINSTANCE hInst;

// 函数声明
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void CreateButtons(HWND hwnd);
bool LaunchApplication(const wchar_t* appPath);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
    hInst = hInstance;

    // 注册窗口类
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

    RegisterClass(&wc);

    // 创建窗口
    HWND hwnd = CreateWindowEx(
        0,
        CLASS_NAME,
        L"系统工具集合",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 350,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    if (hwnd == NULL) {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // 消息循环
    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_CREATE:
        CreateButtons(hwnd);
        return 0;

    case WM_COMMAND:
        switch (LOWORD(wParam)) {
        case ID_TASKMGR:
            LaunchApplication(L"taskmgr.exe");
            break;
        case ID_EXPLORER:
            LaunchApplication(L"explorer.exe");
            break;
        case ID_SNIPPING:
            LaunchApplication(L"SnippingTool.exe");
            break;
        case ID_PAINT:
            LaunchApplication(L"mspaint.exe");
            break;
        case ID_CALC:
            LaunchApplication(L"calc.exe");
            break;
        }
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

void CreateButtons(HWND hwnd) {
    const int buttonWidth = 150;
    const int buttonHeight = 40;
    const int startX = 50;
    const int startY = 30;
    const int spacing = 50;

    CreateWindow(
        L"BUTTON", L"任务管理器",
        WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
        startX, startY, buttonWidth, buttonHeight,
        hwnd, (HMENU)ID_TASKMGR, hInst, NULL
    );

    CreateWindow(
        L"BUTTON", L"文件管理器",
        WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
        startX, startY + spacing, buttonWidth, buttonHeight,
        hwnd, (HMENU)ID_EXPLORER, hInst, NULL
    );

    CreateWindow(
        L"BUTTON", L"截图工具",
        WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
        startX, startY + spacing * 2, buttonWidth, buttonHeight,
        hwnd, (HMENU)ID_SNIPPING, hInst, NULL
    );

    CreateWindow(
        L"BUTTON", L"画图",
        WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
        startX, startY + spacing * 3, buttonWidth, buttonHeight,
        hwnd, (HMENU)ID_PAINT, hInst, NULL
    );

    CreateWindow(
        L"BUTTON", L"计算器",
        WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
        startX, startY + spacing * 4, buttonWidth, buttonHeight,
        hwnd, (HMENU)ID_CALC, hInst, NULL
    );
}

bool LaunchApplication(const wchar_t* appPath) {
    HINSTANCE result = ShellExecute(NULL, L"open", appPath, NULL, NULL, SW_SHOWNORMAL);
    
    if ((INT_PTR)result <= 32) {
        MessageBox(NULL, (std::wstring(L"无法启动应用程序: ") + appPath).c_str(), 
                   L"错误", MB_ICONERROR);
        return false;
    }
    
    return true;
}
